package com.yitongmao.adapter;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.google.gson.Gson;
import com.yitongmao.AppConstancts;
import com.yitongmao.bean.CanCleIntentBean;
import com.yitongmao.bean.DaiFuKuanBean;
import com.yitongmao.bean.LoginHuancunBean;
import com.yitongmao.common.CallBackListener;
import com.yitongmao.core.cache.ACache;
import com.yitongmao.core.network.service.BaseService;
import com.yitongmao.util.SignUtil;
import com.yitongmao.util.ToastUtils;
import com.yitongmao.yitmfruit.R;
import com.zhy.autolayout.AutoLinearLayout;

import java.lang.ref.WeakReference;
import java.util.List;
import java.util.Map;
import java.util.Random;
import java.util.TreeMap;

/*
* 作者：梁伟航 on 2017/7/31 17:59
* 类的用途：
*/
public class DaiFuKuanAdapter extends BaseAdapter {
    private Context context;
    private Dialog bottomDialog;
    private int yuanyinNum = 1;
    WeakReference<Activity> weak;
    private List<DaiFuKuanBean.OrderListBean> orderList;
    private Activity activity;
    private AutoLinearLayout mShoppingNotDelivery;
    private AutoLinearLayout mCaoZuoYouWu;
    private AutoLinearLayout mChongFu;
    private AutoLinearLayout qiTa;
    private AutoLinearLayout mBuXiangMai;
    private AutoLinearLayout qiTaYuanYin;
    private CheckBox mCheckhoppingNotDelivery;
    private CheckBox mCheckCaoZuoYouWu;
    private CheckBox mCheckChongFu;
    private CheckBox mCheckQiTa;
    private CheckBox mCheckBuXiangMai;
    private CheckBox mCheckQiTaYuanYin;
    private LoginHuancunBean loginHuancunBean;
    private int loginId1;
    private TextView mNotSD;
    private TextView mCZYW;
    private TextView mCfxd;
    private TextView mQTPY;
    private TextView mbxm;
    private TextView mQTYY;
    private Button submit;
    private int orderId;
    private String orderCode;

    public DaiFuKuanAdapter(Context context, List<DaiFuKuanBean.OrderListBean> orderList) {
        this.context = context;
        this.orderList = orderList;
        this.weak = new WeakReference<Activity>((Activity) context);
        Gson gson = new Gson();
        //解析查找的缓存并判断
        String loginId = ACache.get(context).getAsString("loginId");
        if (loginId != null) {
            loginHuancunBean = gson.fromJson(loginId, LoginHuancunBean.class);
            loginId1 = loginHuancunBean.userInfo.loginId;
        }
    }

    @Override
    public int getCount() {
        return orderList.size();
    }

    @Override
    public Object getItem(int position) {
        return orderList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder;
        activity = weak.get();
        if (convertView == null) {
            viewHolder = new ViewHolder();
            convertView = View.inflate(context, R.layout.daifahuoitem, null);
            //付款状态
            viewHolder.mFuKuanZhuangTai = (TextView) convertView.findViewById(R.id.mFuKuanZhuangTai);
            //图片
            viewHolder.homelistimage = (ImageView) convertView.findViewById(R.id.homelistimage);
            //title
            viewHolder.homelistTitle = (TextView) convertView.findViewById(R.id.homelistTitle);
            //价钱
            viewHolder.homelistprice = (TextView) convertView.findViewById(R.id.homelistprice);
            //再次购买
            viewHolder.mMyAgainBuy = (Button) convertView.findViewById(R.id.mMyAgainBuy);
            //申请退款
            viewHolder.mMyApplyRefund = (Button) convertView.findViewById(R.id.mMyApplyRefund);
            //去付款
            viewHolder.mMyGoPay = (Button) convertView.findViewById(R.id.mMyGoPay);
            //取消订单
            viewHolder.mMyCanCle = (Button) convertView.findViewById(R.id.mMyCanCle);
            convertView.setTag(viewHolder);//绑定ViewHolder对象
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }
        viewHolder.mFuKuanZhuangTai.setText(orderList.get(position).stateName);
        viewHolder.homelistTitle.setText(orderList.get(position).orderGoods.get(0).title);
        Glide.with(context).load(orderList.get(position).orderGoods.get(0).goodsThumb).into(viewHolder.homelistimage);
        viewHolder.homelistprice.setText(orderList.get(position).orderGoods.get(0).tradePrice + "");
        //待付款
        for (int i = 0; i < orderList.size(); i++) {
            if (orderList.get(i).state.equals("00")) {
                //取消订单
                viewHolder.mMyCanCle.setVisibility(View.VISIBLE);
                //去付款
                viewHolder.mMyGoPay.setVisibility(View.VISIBLE);
                //价钱
                viewHolder.homelistprice.setVisibility(View.GONE);
                //再次购买
                viewHolder.mMyAgainBuy.setVisibility(View.GONE);
                //申请退款
                viewHolder.mMyApplyRefund.setVisibility(View.GONE);
            }
        }
        viewHolder.mMyCanCle.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                orderCode = orderList.get(position).orderCode;
                shoppingCloseAnAccount();
                diaLog();
            }
        });
        return convertView;
    }

    private void diaLog() {
        bottomDialog = new Dialog(context, R.style.BottomDialog);
        View contentView = LayoutInflater.from(context).inflate(R.layout.alert_dialog3, null);
       /* contentView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                bottomDialog.dismiss();
            }
        });*/
        //点击屏幕外边消失dialog
        contentView.findViewById(R.id.lala).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                bottomDialog.dismiss();
            }
        });
        mShoppingNotDelivery = (AutoLinearLayout) contentView.findViewById(R.id.mShoppingNotDelivery);
        mCaoZuoYouWu = (AutoLinearLayout) contentView.findViewById(R.id.mCaoZuoYouWu);
        mChongFu = (AutoLinearLayout) contentView.findViewById(R.id.mChongFu);
        qiTa = (AutoLinearLayout) contentView.findViewById(R.id.QiTa);
        mBuXiangMai = (AutoLinearLayout) contentView.findViewById(R.id.mBuXiangMai);
        qiTaYuanYin = (AutoLinearLayout) contentView.findViewById(R.id.QiTaYuanYin);

        mCheckhoppingNotDelivery = (CheckBox) contentView.findViewById(R.id.mCheckhoppingNotDelivery);
        mCheckCaoZuoYouWu = (CheckBox) contentView.findViewById(R.id.mCheckCaoZuoYouWu);
        mCheckChongFu = (CheckBox) contentView.findViewById(R.id.mCheckChongFu);
        mCheckQiTa = (CheckBox) contentView.findViewById(R.id.mCheckQiTa);
        mCheckBuXiangMai = (CheckBox) contentView.findViewById(R.id.mCheckBuXiangMai);
        mCheckQiTaYuanYin = (CheckBox) contentView.findViewById(R.id.mCheckQiTaYuanYin);
        bottomDialog.setContentView(contentView);

        mNotSD = (TextView) contentView.findViewById(R.id.mNotSD);
        mCZYW = (TextView) contentView.findViewById(R.id.mCZYW);
        mCfxd = (TextView) contentView.findViewById(R.id.mCfxd);
        mQTPY = (TextView) contentView.findViewById(R.id.mQTPY);
        mbxm = (TextView) contentView.findViewById(R.id.mbxm);
        mQTYY = (TextView) contentView.findViewById(R.id.mQTYY);
        submit = (Button) contentView.findViewById(R.id.submit);

        ViewGroup.LayoutParams layoutParams = contentView.getLayoutParams();
        layoutParams.width = activity.getResources().getDisplayMetrics().widthPixels;
        contentView.setLayoutParams(layoutParams);
        bottomDialog.getWindow().setGravity(Gravity.BOTTOM);
        bottomDialog.getWindow().setWindowAnimations(R.style.BottomDialog_Animation);
        bottomDialog.show();
        setOnClick();
    }

    private void setOnClick() {
        submit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                submitOrder(orderCode,String.valueOf(yuanyinNum));
            }
        });
        mCheckhoppingNotDelivery.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                yuanyinNum=1;
                //mCheckhoppingNotDelivery.setChecked(true);
                mCheckCaoZuoYouWu.setChecked(false);
                mCheckChongFu.setChecked(false);
                mCheckQiTa.setChecked(false);
                mCheckBuXiangMai.setChecked(false);
                mCheckQiTaYuanYin.setChecked(false);
            }
        });
        mCheckCaoZuoYouWu.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                yuanyinNum=2;

                mCheckhoppingNotDelivery.setChecked(false);
                //mCheckCaoZuoYouWu.setChecked(false);
                mCheckChongFu.setChecked(false);
                mCheckQiTa.setChecked(false);
                mCheckBuXiangMai.setChecked(false);
                mCheckQiTaYuanYin.setChecked(false);
            }
        });
        mCheckChongFu.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                yuanyinNum=3;

                mCheckhoppingNotDelivery.setChecked(false);
                mCheckCaoZuoYouWu.setChecked(false);
                //mCheckChongFu.setChecked(false);
                mCheckQiTa.setChecked(false);
                mCheckBuXiangMai.setChecked(false);
                mCheckQiTaYuanYin.setChecked(false);
            }
        });
        mCheckQiTa.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                yuanyinNum=4;

                mCheckhoppingNotDelivery.setChecked(false);
                mCheckCaoZuoYouWu.setChecked(false);
                mCheckChongFu.setChecked(false);
                //mCheckQiTa.setChecked(false);
                mCheckBuXiangMai.setChecked(false);
                mCheckQiTaYuanYin.setChecked(false);
            }
        });
        mCheckBuXiangMai.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                yuanyinNum=5;

                mCheckhoppingNotDelivery.setChecked(false);
                mCheckCaoZuoYouWu.setChecked(false);
                mCheckChongFu.setChecked(false);
                mCheckQiTa.setChecked(false);
                //mCheckBuXiangMai.setChecked(false);
                mCheckQiTaYuanYin.setChecked(false);
            }
        });
        mCheckQiTaYuanYin.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                yuanyinNum=6;
                mCheckhoppingNotDelivery.setChecked(false);
                mCheckCaoZuoYouWu.setChecked(false);
                mCheckChongFu.setChecked(false);
                mCheckQiTa.setChecked(false);
                mCheckBuXiangMai.setChecked(false);
                //mCheckQiTaYuanYin.setChecked(false);
            }
        });


        mShoppingNotDelivery.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                yuanyinNum=1;

                mCheckhoppingNotDelivery.setChecked(true);
                mCheckCaoZuoYouWu.setChecked(false);
                mCheckChongFu.setChecked(false);
                mCheckQiTa.setChecked(false);
                mCheckBuXiangMai.setChecked(false);
                mCheckQiTaYuanYin.setChecked(false);
            }
        });
        mCheckCaoZuoYouWu.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                yuanyinNum=2;

                mCheckhoppingNotDelivery.setChecked(false);
                mCheckCaoZuoYouWu.setChecked(true);
                mCheckChongFu.setChecked(false);
                mCheckQiTa.setChecked(false);
                mCheckBuXiangMai.setChecked(false);
                mCheckQiTaYuanYin.setChecked(false);
            }
        });
        mChongFu.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                yuanyinNum=3;

                mCheckhoppingNotDelivery.setChecked(false);
                mCheckCaoZuoYouWu.setChecked(false);
                mCheckChongFu.setChecked(true);
                mCheckQiTa.setChecked(false);
                mCheckBuXiangMai.setChecked(false);
                mCheckQiTaYuanYin.setChecked(false);

            }
        });
        qiTa.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                yuanyinNum=4;

                mCheckhoppingNotDelivery.setChecked(false);
                mCheckCaoZuoYouWu.setChecked(false);
                mCheckChongFu.setChecked(false);
                mCheckQiTa.setChecked(true);
                mCheckBuXiangMai.setChecked(false);
                mCheckQiTaYuanYin.setChecked(false);

            }
        });
        mBuXiangMai.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                yuanyinNum=5;

                mCheckhoppingNotDelivery.setChecked(false);
                mCheckCaoZuoYouWu.setChecked(false);
                mCheckChongFu.setChecked(false);
                mCheckQiTa.setChecked(false);
                mCheckBuXiangMai.setChecked(true);
                mCheckQiTaYuanYin.setChecked(false);
            }
        });
        qiTaYuanYin.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                yuanyinNum=6;
                mCheckhoppingNotDelivery.setChecked(false);
                mCheckCaoZuoYouWu.setChecked(false);
                mCheckChongFu.setChecked(false);
                mCheckQiTa.setChecked(false);
                mCheckBuXiangMai.setChecked(false);
                mCheckQiTaYuanYin.setChecked(true);
            }
        });
    }

    //取消订单原因
    public void shoppingCloseAnAccount() {
        Map params = new TreeMap();
        String timestamp = String.valueOf(System.currentTimeMillis());
        String nonce = String.valueOf(new Random().nextInt());
        params.put("timestamp", timestamp);
        params.put("nonce", nonce);
        params.put("signLogin", SignUtil.signLogin(nonce, timestamp));
        params.put("loginId", String.valueOf(loginId1));
        params.put("sign", SignUtil.sign(params)); //这里也需要时完整的url
        BaseService.getInstance().simplePostRequest(AppConstancts.Net.canCleOrder, params, new CallBackListener() {
            @Override
            public void onStart() {
            }

            @Override
            public void onSuccess(Object result) {
                Log.e("onSuccess", result.toString());
                String string = (String) result;
                Gson gson = new Gson();
                CanCleIntentBean canCleIntentBean = gson.fromJson(string, CanCleIntentBean.class);
                if (canCleIntentBean.result.state.equals("1")) {
                    mNotSD.setText(canCleIntentBean.reason.get(0).reason);
                    mCZYW.setText(canCleIntentBean.reason.get(1).reason);
                    mCfxd.setText(canCleIntentBean.reason.get(2).reason);
                    mQTPY.setText(canCleIntentBean.reason.get(3).reason);
                    mbxm.setText(canCleIntentBean.reason.get(4).reason);
                    mQTYY.setText(canCleIntentBean.reason.get(5).reason);
                }
            }

            @Override
            public void onFail(Object result) {
                Log.e("zzz", "失败" + result.toString());
                System.out.print("失败");
            }
        });
    }
    //提交取消订单的原因
    public void submitOrder(String orderCode,String id){
        Map params = new TreeMap();
        String timestamp = String.valueOf(System.currentTimeMillis());
        String nonce = String.valueOf(new Random().nextInt());
        params.put("timestamp", timestamp);
        params.put("nonce", nonce);
        params.put("orderCode",orderCode);
        params.put("id",id);
        params.put("signLogin", SignUtil.signLogin(nonce, timestamp));
        params.put("loginId", String.valueOf(loginId1));
        params.put("sign", SignUtil.sign(params)); //这里也需要时完整的url
        BaseService.getInstance().simplePostRequest(AppConstancts.Net.submitOrder, params, new CallBackListener() {
            @Override
            public void onStart() {}

            @Override
            public void onSuccess(Object result) {
                Log.e("onSuccess", result.toString());
                String string = (String) result;
                Gson gson = new Gson();
                CanCleIntentBean canCleIntentBean = gson.fromJson(string, CanCleIntentBean.class);
                if (canCleIntentBean.result.state.equals("1")) {
                    if (canCleIntentBean.reason!=null){
                        mNotSD.setText(canCleIntentBean.reason.get(0).reason);
                        mCZYW.setText(canCleIntentBean.reason.get(1).reason);
                        mCfxd.setText(canCleIntentBean.reason.get(2).reason);
                        mQTPY.setText(canCleIntentBean.reason.get(3).reason);
                        mbxm.setText(canCleIntentBean.reason.get(4).reason);
                        mQTYY.setText(canCleIntentBean.reason.get(5).reason);
                    }
                    bottomDialog.dismiss();
                    notifyDataSetChanged();
                }
                ToastUtils.showShort(context,""+canCleIntentBean.result.message);
            }
            @Override
            public void onFail(Object result) {
                Log.e("zzz", "失败" + result.toString());
                System.out.print("失败");
            }
        });
    }

    class ViewHolder {
        public TextView mFuKuanZhuangTai;
        public TextView homelistTitle;
        public ImageView homelistimage;
        public TextView homelistprice;
        public Button mMyAgainBuy;
        public Button mMyApplyRefund;
        public Button mMyGoPay;
        public Button mMyCanCle;
    }
}
